package Thread;

class MyThread2 extends Thread{
    @Override//不写这个注解也可以，我们写上是因为方便编译器对代码进行自动检查
    public void run() {
        while(true){
            System.out.println("hello thread");
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                //为什么这里不能throws异常？
                //如果加上throws就修改了方法签名，此时无法构成重写，父类的run，没有throws这个异常，子类重写的时候，也就不能throwa异常
                throw new RuntimeException(e);
            }
        }
    }
}
public class ThreadDemo2 {
    public static void main(String[] args) throws InterruptedException {
        MyThread t=new MyThread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("hello thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }, "这是我的线程");
        t.start();
        while(true){
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }

}
